/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sync.merge;

import java.util.ArrayDeque;
import java.util.HashMap;
import java.util.Queue;
import sync.exceptions.MergeException;
import sync.local.FileSystemMergeProvider;
import sync.online.PicasaWebAlbumMergeProvider;

/**
 *
 * @author zalem1
 */
public class MergeManager<TLocal, TRemote> {
    private Queue<WatchEntry> _pendingChanges = new ArrayDeque<WatchEntry>(50);
    private MergeChangesThread _mergeThread;
    private HashMap<String, String> _contentOwnership = new HashMap<String, String>();
    private FileSystemMergeProvider _localRepository = new FileSystemMergeProvider(_contentOwnership);
    private PicasaWebAlbumMergeProvider _onlineRepository = new PicasaWebAlbumMergeProvider(_contentOwnership);
    
    
    public void merge(String localPath) {
        synchronized (this) {
            if (isPedningChanges(localPath)) return;
            
            FileSystemMergeProvider localRepository = getLocalRepository();
            PicasaWebAlbumMergeProvider onlineRepository = getOnlineRepository();

            WatchEntry newWatchEntry = new WatchEntry(localPath, localRepository, onlineRepository);
            _pendingChanges.add(newWatchEntry);
        }
    }
    
    private boolean isPedningChanges(String localPath) {
        for (WatchEntry watchEntry : _pendingChanges) {
            if (watchEntry.getLocalPath().equals(localPath)) {
                return true;
            }
        }       
        
        return false;
    }
    
    public WatchEntry getMergeEntry() {
        synchronized (this) {
            return _pendingChanges.poll();
        }
    }
    
    public void start() {
        _mergeThread = new MergeChangesThread(this);
        _mergeThread.setName("Merge changes thread");
        _mergeThread.start();
    }
    
    public void stop() {
        _mergeThread.interrupt();
        _mergeThread = null;
    }

    /**
     * @return the _localRepository
     */
    public FileSystemMergeProvider getLocalRepository() {
        return _localRepository;
    }

    /**
     * @return the _onlineRepository
     */
    public PicasaWebAlbumMergeProvider getOnlineRepository() {
        return _onlineRepository;
    }

    public void UnwatchAlbum(String localPath) {
        throw new UnsupportedOperationException("Not yet implemented");
    }
    
    public void WatchAlbum(String localPath, String owner) {
        throw new UnsupportedOperationException("Not yet implemented");
    }

    public void refresh() {
        throw new UnsupportedOperationException("Not yet implemented");
    }

    public void changeLocalPath(String localPath, String newLocalPath) throws MergeException {
        _localRepository.changeLocalPath(localPath, newLocalPath);
        _onlineRepository.changeLocalPath(localPath, newLocalPath);
        
        String contentOwner = _contentOwnership.get(localPath);
        _contentOwnership.remove(localPath);
        _contentOwnership.put(newLocalPath, contentOwner);
    }
}
